package com.course.component.util;

import cn.hutool.core.map.MapUtil;
import com.course.component.constant.TokenConstants;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liudi
 * @Description: 获取用户信息工具
 * @date 2020-06-11 09:26
 */
public class CurrentUserUtil {

    private static final ThreadLocal<Map<String, Object>> threadContext = ThreadLocal.withInitial(() -> new HashMap<>());

    private CurrentUserUtil() {

    }

    /**
     * 取得thread context Map的实例。
     *
     * @return thread context Map的实例
     */
    public static Map<String, Object> getContextMap() {
        return threadContext.get();
    }

    public static String getToken() {
        return MapUtil.getStr(getContextMap(), TokenConstants.INTERSTELLAR_TOKEN);
    }



    public static String getUserID() {
        return MapUtil.getStr(getContextMap(), TokenConstants.KEY_USER_ID);
    }


    public static String getTelephone() {
        return MapUtil.getStr(getContextMap(), TokenConstants.KEY_TELEPHONE);
    }

    public static String getRequestSource() {
        return MapUtil.getStr(getContextMap(), TokenConstants.KEY_APP_TYPE);
    }


    public static void setToken(String token) {
        getContextMap().put(TokenConstants.INTERSTELLAR_TOKEN, token);
    }


    public static void setUserID(String userID) {
        getContextMap().put(TokenConstants.KEY_USER_ID, userID);
    }

    public static void setTelephone(String telephone) {
        getContextMap().put(TokenConstants.KEY_TELEPHONE, telephone);
    }

    public static void setRequestSource(String requestSource) {
        getContextMap().put(TokenConstants.KEY_APP_TYPE, requestSource);
    }



    /**
     * 根据key删除值
     *
     * @param key
     * @return
     */
    public static Object remove(String key) {
        return getContextMap().remove(key);
    }

    public static void remove() {
        getContextMap().clear();
    }

}
